﻿<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             x:Class="Schedulr.Views.StatusPanel"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d" 
             DataContext="{Binding Path=StatusPanelViewModel, Source={StaticResource Locator}}">
    <DockPanel LastChildFill="True">
        <DockPanel.Style>
            <Style TargetType="DockPanel" BasedOn="{StaticResource {x:Type DockPanel}}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=ExecutingTasks.Count}" Value="0">
                        <Setter Property="Visibility" Value="Collapsed" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DockPanel.Style>
        <Border Margin="5" Padding="5" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1,0,0,0">
            <ItemsControl ItemsSource="{Binding ExecutingTasks}" VerticalAlignment="Center">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <DockPanel LastChildFill="True">
                            <StackPanel DockPanel.Dock="Left" Orientation="Horizontal" VerticalAlignment="Center">
                                <StackPanel.Style>
                                    <Style TargetType="StackPanel" BasedOn="{StaticResource {x:Type StackPanel}}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=Task.IsComplete, Mode=OneWay}" Value="True">
                                                <Setter Property="Visibility" Value="Collapsed" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </StackPanel.Style>
                                <ProgressBar Value="{Binding Path=Task.PercentComplete, Mode=OneWay}" Minimum="0" Maximum="1" Width="50" Height="10" Margin="0,0,5,0">
                                    <ProgressBar.Style>
                                        <Style TargetType="ProgressBar" BasedOn="{StaticResource {x:Type ProgressBar}}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Path=Task.IsError, Mode=OneWay}" Value="True">
                                                    <Setter Property="Foreground" Value="{DynamicResource ErrorForegroundBrush}" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Path=Task.PercentComplete, Mode=OneWay}" Value="{x:Null}">
                                                    <Setter Property="IsIndeterminate" Value="True" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                        </ProgressBar.Style>
                                </ProgressBar>
                                <TextBlock Text="{Binding Path=Task.PercentComplete, Mode=OneWay, StringFormat='{}{0:00%} | '}" Foreground="{DynamicResource UserMessageFontBrush}" FontSize="12" TextTrimming="CharacterEllipsis" />
                            </StackPanel>
                            <TextBlock DockPanel.Dock="Left" FontSize="12" VerticalAlignment="Center" TextTrimming="CharacterEllipsis">
                                <Hyperlink Command="{Binding ShowDetailsCommand}" ToolTip="Click for details">
                                    <Hyperlink.Style>
                                        <Style TargetType="Hyperlink" BasedOn="{StaticResource {x:Type Hyperlink}}">
                                            <Style.Triggers>
                                                <!-- Apparently the default Foreground value needs to be set as well, if we specify it on the hyperlink directly then the data trigger doesn't work -->
                                                <DataTrigger Binding="{Binding Path=Task.IsError, Mode=OneWay}" Value="False">
                                                    <Setter Property="Foreground" Value="{DynamicResource UserMessageFontBrush}" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Path=Task.IsError, Mode=OneWay}" Value="True">
                                                    <Setter Property="Foreground" Value="{DynamicResource ErrorForegroundBrush}" />
                                                </DataTrigger>
                                                <Trigger Property="IsEnabled" Value="False">
                                                    <Setter Property="TextDecorations" Value="None" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Hyperlink.Style>
                                    <TextBlock Text="{Binding Path=Task.Name}" TextTrimming="CharacterEllipsis" />
                                </Hyperlink>
                            </TextBlock>
                            <Popup Placement="Mouse" StaysOpen="False" IsOpen="{Binding DetailsVisible}">
                                <Border Width="400" Height="200" Padding="2" BorderBrush="{DynamicResource BorderBrush}" Background="{DynamicResource DiagramGradientBrush}" BorderThickness="1">
                                    <DockPanel LastChildFill="True">
                                        <Label DockPanel.Dock="Top" Content="{Binding Path=Task.Name, Mode=OneWay}" Padding="0" />
                                        <Thumb DockPanel.Dock="Bottom" Cursor="SizeNWSE" HorizontalAlignment="Right" DragDelta="OnPopupDragDelta" Style="{DynamicResource ResizeGripThumbStyle}" />
                                        <TextBox IsReadOnly="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Text="{Binding Path=StatusHistoryDescription, Mode=OneWay}" TextChanged="statusHistoryTextBox_TextChanged" />
                                    </DockPanel>
                                </Border>
                            </Popup>
                            <TextBlock Foreground="{DynamicResource UserMessageFontBrush}" FontSize="12" Text="{Binding Path=Task.Status, StringFormat=' : {0}'}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis">
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=Task.Status}" Value="{x:Null}">
                                                <Setter Property="Visibility" Value="Collapsed" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </DockPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Border>
    </DockPanel>

</UserControl>